package cn.imvc.app.models;

import java.util.List;
import java.util.Set;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import cn.imvc.app.entities.Department;
import cn.imvc.app.repositories.DepartmentRepository;
import cn.imvc.core.DialogBean;
import cn.imvc.core.HttpContext;


@Service
public class DepartmentModelImpl implements DepartmentModel {
	@Autowired
	private DepartmentRepository departmentRepository;

	public Department save(int id,Department department) {
		department.setParent(get(id));
		return departmentRepository.saveAndFlush(department);
	}

	public Department update(int id, Department newdepartment) {
		Department loadDepartment = this.get(id);
		
		BeanUtils.copyProperties(newdepartment, loadDepartment, new String[] {
				"id", "parent" });
		
		
		return departmentRepository.saveAndFlush(loadDepartment);
	}

	public DialogBean delete(int id) {
		DialogBean dialogBean = new DialogBean();
		if (get(id).getDepartment().size() > 0) {
			dialogBean.setStatusCode("300");
			dialogBean.setMessage("有下级部门不能删除");
		} else {
			dialogBean.setStatusCode("200");
			dialogBean.setMessage("删除成功");
			dialogBean.setDialogId("department");
			departmentRepository.delete(id);
		}
		
		return dialogBean;
	}

	public Department get(int id) {
		return departmentRepository.findOne(id);
	}

	public String getDepartmentTree() {
		String rootUrl = HttpContext.getContext().getAttribute("rootUrl")
				.toString();
		Set<Department> departments = departmentRepository.findByParentIsNull();
		StringBuffer tree = new StringBuffer();
		for (Department department : departments) {
			tree.append("<li class='treeli' data_id='" + department.getId()
					+ "'><a href='" + rootUrl + "/system/department/"
					+ department.getId()
					+ "/edit' target='ajax' rel='jbsxBox'>"
					+ department.getName() + "</a>");
			tree.append(this.getChildDepartment(department.getId(), rootUrl));
			tree.append("</li>");
		}
		return tree.toString();
	}

	@SuppressWarnings("unchecked")
	private String getChildDepartment(int id, String rootUrl) {
		Set<Department> departments = get(id)
				.getDepartment();
		StringBuffer childNode = new StringBuffer();
		if (departments.size() > 0) {
			childNode.append("<ul>");
			for (Department department : departments) {
				childNode.append("<li class='treeli' data_id='"
						+ department.getId() + "'><a href='" + rootUrl
						+ "/system/department/" + department.getId()
						+ "/edit' target='ajax' rel='jbsxBox'>"
						+ department.getName() + "</a>");
				childNode.append(this.getChildDepartment(department.getId(),
						rootUrl));
				childNode.append("</li>");
			}
			childNode.append("</ul>");
		}
		return childNode.toString();
	}

	public List<Department> find() {
		return departmentRepository.findAll(new Sort(Direction.DESC,"order"));
	}

	public String treeLookup() {
		Set<Department> departments = departmentRepository.findByParentIsNull();
		StringBuffer tree = new StringBuffer();
		for (Department department : departments) {
			tree.append("<li><a href=\"javascript:\" onclick=\"$.bringBack({code:'"+department.getId()+"', name:'"+department.getName()+"'})\">"+department.getName()+"</a>");
			tree.append(this.getChildDep(department));
			tree.append("</li>");
		}
		return tree.toString();
	}
	private String getChildDep(Department department){
		Set<Department> departments = department.getDepartment();
		StringBuffer childNode = new StringBuffer();
		if (departments.size() > 0) {
			childNode.append("<ul>");
			for (Department childDepartment : departments) {
				childNode.append("<li><a href=\"javascript:\" onclick=\"$.bringBack({code:'"+childDepartment.getId()+"', name:'"+childDepartment.getName()+"'})\">"+childDepartment.getName()+"</a>");
				childNode.append(this.getChildDep(childDepartment));
				childNode.append("</li>");
			}
			childNode.append("</ul>");
		}
		return childNode.toString();
	}

	public String lookupSuggest() {
		List<Department> departments = departmentRepository.findAll();
		StringBuffer text=new StringBuffer();
		text.append("[");
		int i=0;
		for (Department department : departments) {
			if(i==0){
				i++;
				text.append("{\"code\":\""+department.getId()+"\", \"name\":\""+department.getName()+"\"}");
			}else{
				text.append(",{\"code\":\""+department.getId()+"\", \"name\":\""+department.getName()+"\"}");
			}
		}
		text.append("]");
		return text.toString();
	}

}
